package com.moqianfan.mcuassistant.fragment.play;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.activity.WebInfoActivity;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.bean.NewsBean;
import com.moqianfan.mcuassistant.bean.NewsBean.NewslistBean;
import com.moqianfan.mcuassistant.comm.Common;
import com.moqianfan.mcuassistant.fragment.BaseFragment;
import com.moqianfan.mcuassistant.net.BaseRetorfit;
import com.moqianfan.mcuassistant.net.ServiceApi;
import com.moqianfan.mcuassistant.utils.GsonUtils;
import com.moqianfan.mcuassistant.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.yalantis.phoenix.PullToRefreshView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnItemClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/6/16.
 * 微信精选
 */

public class JokeFragment extends BaseFragment {

    @BindView(R.id.list_wx)
    ListView listWx;
    @BindView(R.id.pull_refresh)
    PullToRefreshView pull;

    private List<NewslistBean> wxNews;
    private CommDataAdapter<NewslistBean> adapter;
    private int page = 1;
    private boolean isButtom = false;

    private static String TAG = "JokeFragment";

    @Override
    public View findViews(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_wxnews, container, false);
    }

    @Override
    public void initViews() {
        wxNews = new ArrayList<>();
        pull.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pull.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        wxNews.clear();
                        getWxNews();
                    }
                }, 1000);
            }
        });

        listWx.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        //滚动到底部
                        if (listWx.getLastVisiblePosition() == (adapter.getCount() - 1)) {
                            LogUtil.w(TAG, "到底部了");
                            isButtom = true;
                        }
                }
                if (isButtom) {
                    getWxNews();
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    @Override
    public void loadData() {
        getWxNews();
    }

    public void getWxNews() {
        LogUtil.w(TAG, "获取微信数据");
        ServiceApi serviceApi = new BaseRetorfit(Common.JOKE_URL).createService(ServiceApi.class);
        Map<String, Object> params = new HashMap<>();
        params.put("page", page++);
        params.put("src","vbbv68");
        params.put("num", 10);
        params.put("word", "笑话");
        LogUtil.w(TAG, params.toString());
        serviceApi.getWxNews(params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NewsBean>() {
                    @Override
                    public void onCompleted() {
                        LogUtil.w(TAG, "完成");
                        updateUI();
                        isButtom = false;
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        LogUtil.w(TAG, "结果=" + throwable.toString());
                    }

                    @Override
                    public void onNext(NewsBean newsBean) {
                        for (NewsBean.NewslistBean item : newsBean.getNewslist()) {
                            LogUtil.w(TAG, "结果=" + GsonUtils.GsonString(item));
                            wxNews.add(item);
                        }
                    }
                });
    }

    public void updateUI() {
        if (null == adapter) {
            adapter = new CommDataAdapter<NewslistBean>(wxNews, R.layout.view_item_wx) {
                @Override
                public void bindView(ViewHolder holder, NewslistBean obj) {
                    holder.setText(R.id.txt_item_desc, obj.getCtime() + " | " + obj.getDescription());
                    holder.setText(R.id.txt_item_title, obj.getTitle());
                    if (!TextUtils.isEmpty(obj.getPicUrl()))
                        Picasso.with(getContext()).load(obj.getPicUrl()).placeholder(R.drawable.default_wx)
                                .into((ImageView) holder.getView(R.id.image_item_url));
                }
            };
            listWx.setAdapter(adapter);
        } else {
            adapter.setmData(wxNews);
        }
        pull.setRefreshing(false);
    }

    @OnItemClick(R.id.list_wx)
    public void OnItemOnClick(AdapterView<?> view, View view1, int i, long l) {
        Intent inten = new Intent(getActivity(), WebInfoActivity.class);
        inten.putExtra("title", wxNews.get(i).getTitle());
        inten.putExtra("url", wxNews.get(i).getUrl());
        inten.putExtra("picUrl", wxNews.get(i).getPicUrl());
        inten.putExtra("desc", wxNews.get(i).getCtime() + " | " + wxNews.get(i).getDescription());
        inten.putExtra("head","段子");
        inten.putExtra("flag", 0);
        startActivity(inten);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
